<?php
class PublicAction extends Action {
	
	public function index(){
		$this->redirect('checklogin');
	}
	
	/**
	 * 登录
	 *
	 */
	public function login(){
		$this->display();
	}
	
	/**
	 * 登录检查
	 *
	 */
	public function checklogin(){
		$username = isset($_POST["username"]) ? $_POST["username"] : Cookie::get("username");
		$password = isset($_POST["password"]) ? md5($_POST["password"]) : Cookie::get("password");
		
		$userDao = D('Characcount');
		$test = $userDao->find("username='$username' and password='$password'",'uid');
		if($test){
			$userDao->setField('lastlogintime',time(),"uid=".$userDao->uid);
			Session::set('uid',$userDao->uid);
			Session::set('userinfo',$userDao);
			Cookie::set('username',$username,86400);
			Cookie::set('password',$password,86400);
			unset($username,$password);
			$char = D('Charbase');
			$charid = $char->find("uid='$userDao->uid'",'cid');
			if($charid){
				Session::set('cid',$char->cid);
				$this->redirect('index','Index');
			}
			else{
				$this->redirect('charcreate');		
			}
		}else{
			$this->redirect('login');			
		}
	}
	
	/**
	 * 登出
	 *
	 */
	public function logout(){
		Session::set('uid','');
		Session::set('cid','');
		Session::set('userinfo','');
		Cookie::set('username',$username,-86400);
		Cookie::set('password',$password,-86400);
		$this->redirect('index','Index');
	}
	
	/**
	 * 注册
	 *
	 */
	public function reg(){
		//绑定Lang文件，这里还需要尝试
		//include('Tpl/lang/public.inc.php');
		//$this->assign('lang',$reg_lang);
		$this->display();
	}
	
	/**
	 * 注册写入
	 *
	 */
	public function doreg(){
		if(isset($_POST["username"]) && isset($_POST["password"]) ){
			$newuser = D("Characcount");
			//$newuser->username=$_POST["username"];
			//$newuser->password=md5($_POST["password"]);
			//$newuser->nicename=$_POST["nicename"];
			//$newuser->email=$_POST["email"];
			//$newuser->secquestion=$_POST["secquestion"];
			//$newuser->secanswer=$_POST["secanswer"];
			if($newuser->create()){
				$userId = $newuser->add();
				$this->uid = $userId;
				$userInfo['username']	=	$newuser->username;
				$userInfo['email']	=	$newuser->email;
				$userInfo['sex']	=	$newuser->sex;
				Session::set('uid',$userId);
				Session::set('userInfo',$userInfo);
				Cookie::set('username',$newuser->username,86400);
				Cookie::set('password',$newuser->password,86400);
				$this->redirect("charcreate","Public");
			}else{
				header("Content-Type:text/html; charset=utf-8");
           		exit($newuser->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');
			}
		}else{
			$this->redirect("reg");
		}
	}
	
	/**
	 * 角色创建
	 *
	 */
	public function charcreate() {
		$uid = Session::get("uid");
		if($uid){
			$char = D('Charbase');
			$existcid = $char->find("uid = $uid");
			$existcid ?	$this->redirect('index','Index') : $this->display();
		}
		else{
			$this->redirect('login','Public');
		}
	}
	
	/**
	 * 角色创建写入
	 *
	 */
	public function docreate(){
		$uid = Session::get("uid");
		if($uid){
			if($name = $_POST['name'] && $sex = $_POST['sex']){
				$newchar = D('Charbase');
				$newplus = D('Charplus');
				if($newchar->create() && $newplus->create()){
					$newchar->uid = $uid;
					$newchar->add();
					$cid = $newplus->add();
					Session::set('cid',$cid);
					$this->redirect('index','Index');
				}
				else {
					header("Content-Type:text/html; charset=utf-8");
           			exit($newchar->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');
				}
			}
			else {
				$this->redirect('charcreate','Public');
			}
		}
		else {
			$this->redirect('login','Public');
		}
	}
	
	/**
	 * 验证码生成，注册使用
	 *
	 */
	public function verify() {
        import("ORG.Util.Image");
       	Image::buildImageVerify(); 
	}
}
?>